package org.standard.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.standard.base.model.BaseUnit;
import org.standard.sys.model.SysFace;
import org.standard.sys.model.SysLog;
import org.standard.commons.utils.DAO;
import org.standard.sys.service.SysFaceService;
import org.standard.sys.service.SysLogService;

public class SysFaceServiceImpl implements SysFaceService {
	private DAO dao;

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
	
	/**
	 * 查询界面表中的记录
	 * @return 如果存在记录则返回该记录；否则的话返回值为null
	 * @author wang zitao
	 */
	public SysFace search() {
		SysFace face=null;

		String hql = " from SysFace";

		List list = dao.find(hql);

		if (list.size() > 0) {
			face=(SysFace)list.get(0);
		}
		return face;
	}
	
	
	public String Modify(SysFace face) {
		// TODO Auto-generated method stub
		String message="";
		if (search()==null) {  //不存在则直接添加
			String faceId = (String) dao.save(face);	
		    if(faceId!=null)
			message="添加成功";
		    else
			 message="添加失败";
		} else {
			ArrayList list=new ArrayList();
			StringBuffer hqlsb=new StringBuffer();
			hqlsb.append("update SysFace set ieTitle=?,statusText=?,bannerText=?,bannerFont=?" );
			list.add((face.getIeTitle()!=null)?face.getIeTitle():"");
			list.add((face.getStatusText()!=null)?face.getStatusText():"");
			list.add((face.getBannerText()!=null)?face.getBannerText():"");
			list.add((face.getBannerFont()!=null)?face.getBannerFont():"宋体");
			
			//判断LOGO是否改变，若有改变，变动数据库中关于LOGO的选项
			if(face.getLogoName()!=null&&!"".equals(face.getLogoName())){
				hqlsb.append(",logoName=?,logoWidth=?,logoHight=?");
				list.add(face.getLogoName());
				list.add((face.getLogoWidth()!=null)?face.getBmpTopWidth():"50");
				list.add((face.getLogoHight()!=null)?face.getLogoHight():"30");
					}
			
			//判断顶部图片是否改变，若有改变，变动数据库中关于顶部图片的选项
			if(face.getBmpTopName()!=null&&!"".equals(face.getBmpTopName())){
				hqlsb.append(",bmpTopName=?,bmpTopWidth=?,bmpTopHight=?");
				list.add(face.getBmpTopName());
				list.add((face.getBmpTopWidth()!=null)?face.getBmpTopWidth():"400");
				list.add((face.getBmpTopHight()!=null)?face.getBmpTopHight():"100");
			}
			
			//判断登录图片是否改变
			if(face.getBmpLoginName()!=null&&!"".equals(face.getBmpLoginName())){
				hqlsb.append(",bmpLoginName=? ");
				list.add(face.getBmpLoginName());
			}
			
			//判断桌面图片是否改变
			if(face.getBmpDeskName()!=null&&!"".equals(face.getBmpDeskName())){
				hqlsb.append(",bmpDeskName=? ");
				list.add(face.getBmpLoginName());
			}
			Integer i=dao.updateBulk(hqlsb.toString(),list.toArray());
			if(i==1)
				 message="更新成功";
			else
				message="更新失败";
		}
		return message;
	}
}
